﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Win32;

/// <summary>
/// 注册表操作类
/// </summary>
public class Class_Registry
{
    /// <summary>
    /// 判断注册表项(相当于目录)是否存在
    /// Reg_Path 例如:software\\media\\pro
    /// </summary>
    /// <param name="Reg_Root"></param>
    /// <param name="Reg_Path"></param>
    /// <returns></returns>
    public bool RegItemExist(RegistryKey Reg_Root, string Reg_Path)
    {
        try
        {
            string[] subkeyNames;
            RegistryKey hkml = Reg_Root; //注册表根节点
            string Reg_Dir = Reg_Path.Substring(Reg_Path.LastIndexOf("\\") + 1);
            Reg_Path = Reg_Path.Substring(0, Reg_Path.LastIndexOf("\\"));
            RegistryKey software = hkml.OpenSubKey(Reg_Path);
            subkeyNames = software.GetSubKeyNames();

            //取得该项下所有子项的名称的序列，并传递给预定的数组中  
            foreach (string keyName in subkeyNames)
            //遍历整个数组  
            {
                Console.WriteLine("键名：" + keyName + "键值" + Reg_Dir);
                if (keyName == Reg_Dir)
                //判断子项的名称  
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }


    }
    /// <summary>
    /// 判断注册表中键是否在存在
    /// </summary>
    /// <returns></returns>
    public bool RegKeyExist(RegistryKey Reg_Root, string Reg_Path, string Reg_Key)
    {
        try
        {
            string[] subkeyNames;
            RegistryKey hkml = Reg_Root;
            RegistryKey software = hkml.OpenSubKey(Reg_Path);
            subkeyNames = software.GetValueNames();
            //取得该项下所有键值的名称的序列，并传递给预定的数组中
            foreach (string keyName in subkeyNames)
            {
                if (keyName == Reg_Key) //判断键值的名称
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }
    /// <summary>
    /// 创建注册表项，失败返回false,成功返回true
    /// </summary>
    /// <param name="Reg_Root"></param>
    /// <param name="Reg_Path"></param>
    /// <returns></returns>
    public bool CreateRegItem(RegistryKey Reg_Root, string Reg_Path)
    {
        try
        {
            RegistryKey Key = Reg_Root;
            RegistryKey SoftWare = Key.CreateSubKey(Reg_Path);
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }
    /// <summary>
    /// 删除注册表项 失败返回false,成功返回true
    /// </summary>
    /// <param name="Reg_Root"></param>
    /// <param name="Reg_Path"></param>
    /// <returns></returns>
    public bool DeleteRegItem(RegistryKey Reg_Root, string Reg_Path)
    {
        try
        {
            RegistryKey Key = Reg_Root;
            Key.DeleteSubKey(Reg_Path);
            Key.Close();
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }
    /// <summary>
    /// 创建或写注册表中键值,★路径必须存在★，创建才能成功
    /// </summary>
    /// <param name="Reg_Root"></param>
    /// <param name="Reg_Path"></param>
    /// <param name="Reg_Key"></param>
    /// <param name="Reg_KeyValue"></param>
    /// <returns></returns>
    public bool CreateRegKey(RegistryKey Reg_Root, string Reg_Path, string Reg_Key, string Reg_KeyValue)
    {
        try
        {
            RegistryKey Key = Reg_Root;
            RegistryKey SoftWare = Key.OpenSubKey(Reg_Path, true);
            SoftWare.SetValue(Reg_Key, Reg_KeyValue);
            Key.Close();
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }
    /// <summary>
    /// 读注册表键值 成功返回内容，失败返回空字符串
    /// </summary>
    /// <param name="Reg_Root"></param>
    /// <param name="Reg_Path"></param>
    /// <param name="Reg_Key"></param>
    /// <returns></returns>
    public string ReadReg(RegistryKey Reg_Root, string Reg_Path, string Reg_Key)
    {
        try
        {
            string info = "";
            RegistryKey Key = Reg_Root;
            RegistryKey myreg = Key.OpenSubKey(Reg_Path);
            info = myreg.GetValue(Reg_Key).ToString();
            myreg.Close();
            return info;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return "";
        }
    }

}
/// <summary>
/// 字符串加密类
/// </summary>
public class Class_Encrypt
{
    #region "定义加密字串变量"
    private SymmetricAlgorithm mCSP = SymmetricAlgorithm.Create();
    private const string CIV = "Mi9l/+7Zujhy12se6Yjy111A";  //初始化向量
    private const string CKEY = "jkHuIy9D/9i="; //密钥（常量）

    #endregion

    

    /// <summary>
    /// 加密字符串
    /// </summary>
    /// <param name="Value">需加密的字符串</param>
    /// <returns></returns>
    public string EncryptString(string Value)
    {
        ICryptoTransform ct; //定义基本的加密转换运算
        MemoryStream ms; //定义内存流
        CryptoStream cs; //定义将内存流链接到加密转换的流
        byte[] byt;

        //CreateEncryptor创建(对称数据)加密对象

        ct = mCSP.CreateEncryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV)); //用指定的密钥和初始化向量创建对称数据加密标准


        byt = Encoding.UTF8.GetBytes(Value); //将Value字符转换为UTF-8编码的字节序列

        ms = new MemoryStream(); //创建内存流
        cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); //将内存流链接到加密转换的流
        cs.Write(byt, 0, byt.Length); //写入内存流
        cs.FlushFinalBlock(); //将缓冲区中的数据写入内存流，并清除缓冲区
        cs.Close(); //释放内存流

        return Convert.ToBase64String(ms.ToArray()); //将内存流转写入字节数组并转换为string字符
    }

    /// <summary>
    /// 解密字符串
    /// </summary>
    /// <param name="Value">要解密的字符串</param>
    /// <returns>string</returns>
    public string DecryptString(string Value)
    {
        ICryptoTransform ct; //定义基本的加密转换运算
        MemoryStream ms; //定义内存流
        CryptoStream cs; //定义将数据流链接到加密转换的流
        byte[] byt;

        ct = mCSP.CreateDecryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV)); //用指定的密钥和初始化向量创建对称数据解密标准
        byt = Convert.FromBase64String(Value); //将Value(Base 64)字符转换成字节数组

        ms = new MemoryStream();
        cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
        cs.Write(byt, 0, byt.Length);
        cs.FlushFinalBlock();
        cs.Close();

        return Encoding.UTF8.GetString(ms.ToArray()); //将字节数组中的所有字符解码为一个字符串
    }
}
/// <summary>
/// INI配置文件读写类
/// </summary>
public class Class_INI
{
    //引用外部函数INI文件读写
    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport("kernel32")]
    ///读INI配置文件
    private static extern int GetPrivateProfileString(string section,
         string key, string def, StringBuilder retVal, int size, string filePath);

    /// <summary>
    /// 读配置文件中的键值
    /// </summary>
    /// <param name="Section">配置文件段落名</param>
    /// <param name="Key">配置文件键名</param>
    /// <param name="INI_Path">配置文件路径和文件名</param>
    /// <param name="DefineValue">读取失败 返回的默认值</param>
    /// <returns></returns>
    public string Read_INI(string Section, string Key, string INI_Path, string DefineValue)
    {
        StringBuilder Str_B = new StringBuilder(255);

        int ret = GetPrivateProfileString(Section, Key, DefineValue, Str_B, 255, INI_Path);
            return Str_B.ToString();
       
    }
    /// <summary>
    /// 写配置文件
    /// </summary>
    /// <param name="Section">配置文件段落名</param>
    /// <param name="Key">配置文件键名</param>
    /// <param name="INI_Path">配置文件路径和文件名</param>
    /// <param name="Valueh">要保存的键值</param>
    /// <returns></returns>
    public bool Write_INI(string Section,string Key,string INI_Path,string Value)
    {
        long ret = WritePrivateProfileString(Section, Key, Value, INI_Path);
        return true;
    }
}
